package com.tywj.buscustomerapp.view.activity;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.MyTicketBean;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ComplaintActivity extends BaseActivity {

    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.head_left)
    ImageView back;
    @BindView(R.id.radiogroup)
    RadioGroup radioGroup;
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.textView7)
    TextView submit;

    private String[] comps = {"司机态度恶劣","司机迟到","司机危险驾驶","乘车体验不好"};
    private MyTicketBean myTicketBean;

    @Override
    protected int getContentId() {
        return R.layout.activity_complaint;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText("投诉");
        if (getIntent()!=null){
            myTicketBean = (MyTicketBean) getIntent().getSerializableExtra("ticket");
        }
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = "";
                int id = radioGroup.getCheckedRadioButtonId();
                switch (id){
                    case R.id.button1:
                        message = comps[0]+" ";
                        break;
                    case R.id.button2:
                        message = comps[0]+" ";
                        break;
                    case R.id.button3:
                        message = comps[0]+" ";
                        break;
                    case R.id.button4:
                        message = comps[0]+" ";
                        break;
                }
                message = message + editText.getText().toString();
                doApplyComp(message.replace(",","，"));
            }
        });
    }
//    ousu(xl:string;tsnr:string;pjr:string)
    private void doApplyComp(final String message){
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e){
                try {
                    String msg = WebServicrUtils.getInstance()
                            .postComp(myTicketBean.getTravelNum(),message,SPUtils.getUserPhone(getApplicationContext()));
                    if (msg.equals("ok")){
                        e.onNext(msg);
                    }else{
                        e.onError(null);
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                    e.onError(null);
                } catch (XmlPullParserException e1) {
                    e1.printStackTrace();
                    e.onError(null);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        ToastJoe.getmToastJoe().ToastShowRight(ComplaintActivity.this,null,"投诉成功");
                    }


                    @Override
                    public void onError(Throwable e) {
                        ToastJoe.getmToastJoe().ToastShowRight(ComplaintActivity.this,null,"上传失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
